<?php
declare (strict_types=1);

namespace app;

use think\App;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\View;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    /**
     * 构造方法
     * @access public
     * @param App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
    }

    /**
     * 跳转页面
     * @param string $msg
     * @param string|null $url
     * @param int $time
     * @return string
     * @author An Yang
     * @time 2020/8/6 9:44
     */
    public function success(string $msg, string $url = "", int $time = 3): string
    {
        $data = [
            'msg' => $msg,
            'url' => $url ?? request()->header('referer'),
            'time' => $time,
        ];
        return $this->fetch('/jump/success', $data);

    }

    /**
     * 错误跳转页面
     * @param string $msg
     * @param string $url
     * @param int $time
     * @return string
     * @author An Yang
     * @time 2020/8/6 9:44
     */
    public function error(string $msg, string $url = "", int $time = 3): string
    {
        $data = [
            'msg' => $msg,
            'url' => $url ?? request()->header('referer'),
            'time' => $time,
        ];
        return $this->fetch('/jump/error', $data);
    }

    /**
     * 输入模板变量
     * @param $name
     * @param $value
     * @return \think\View
     */
    protected function assign($name, $value = null)
    {
        return View::assign($name, $value);
    }

    /**
     * 模板引擎输出
     * @param string $template
     * @param array $vars
     * @return string
     * @author An Yang
     * @time 2020/7/24 14:50
     */
    protected function fetch(string $template = '', array $vars = []): string
    {
        $name = 'ay';
        //载入当前选择模板配置
        $path = 'public' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . $name;

        //获取模型与控制器名称
        $controller = app('http')->getName() . DIRECTORY_SEPARATOR . request()->controller(true);
        //实际模板文件
        $file = root_path() . $path . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . $template . '.' . Config::get('view.view_suffix');

        //检验当前模板中模板文件是否存在
        if (!is_file($file)) {
            $path = 'public' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR . 'default';
            $name = 'default';
        }

        $config = [
            'view_dir_name' => $path,
            'tpl_replace_string' => [
                '__VIEW__' => '/view/' . $name,
                '__Tpl__' => "/static/" . $path,
            ],
        ];
        Config::set($config, "view");
        return View::fetch($template, $vars);
    }

}
